<html>
<head>
<script>
	var selectText = '';
	chrome.extension.onConnect.addListener(function(a){
		a.onMessage.addListener(function(ab){
			if(ab.message=="select")
				selectText=ab.txt;
		});
	});

	chrome.browserAction.onClicked.addListener(function(tab) {		
        // We need to know if we are the active window, because the tab may
        // have moved to another window and we don't want to execute this
        // action multiple times.
        
    	var URL = '';
    	var mailURL = 'mail.google.com';
    	var bodyText = '';
    	if(selectText!='')
    		bodyText = selectText+"\r\n\r\n"+tab.url;
    	else
			bodyText = tab.url;
    	if(localStorage["gr_use_gmailPage"] == "true")
		{
			if(localStorage["gc_domain"] != null && localStorage["gc_domain"].length > 0)
		        mailURL = localStorage["gc_domain"];
		        
		    if(mailURL[mailURL.length - 1] != "/")
		        mailURL += "/"; // Add trailing slash
			URL = "https://" + mailURL + "mail?view=cm&tf=0&su="+encodeURI(tab.title)+"&body="+encodeURI(bodyText);
	        chrome.tabs.create({url: URL});
		}
		else 
    	{
			URL = "mailto:?SUBJECT="+encodeURI(tab.title)+"&BODY="+encodeURI(bodyText);
	        chrome.tabs.update(tab.id,{url: URL});
		}
	});
  
</script>
</head>
</html>